package com.dabai.simpleconnection_udp.log;

import com.dabai.simpleconnection_udp.utils.DateUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 实用程序日志
 *
 * @author Flower3
 * @date 2023/08/31
 */
public class LogUtils {

    private static String log_path;

    /**
     * 初始化
     *
     * @param logPath 日志路径
     */
    public static void init(String logPath){
        log_path = logPath;
    }

    /**
     * 获取路径
     *
     * @return {@link String}
     */
    public static String getPath(){
        return log_path;
    }
    public static File getFile(){
        return new File(log_path);
    }

    /**
     * 追加日志
     *
     * @param tag     标签
     * @param content 所容纳之物
     */
    public static void appendLog(LogTagEnum tag, String content){

        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(log_path, true));
            String logStr = "["+ DateUtils.getNowTime(1) + "] "+tag.name()+"\n"+content;
            bufferedWriter.write(logStr);
            bufferedWriter.newLine();
            bufferedWriter.newLine();
            bufferedWriter.flush();
            bufferedWriter.close();
            System.out.println("A new line has been appended to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred while appending the new line to the file.");
            e.printStackTrace();
        }
    }

    /**
     * 追加日志
     *
     * @param tag     标签
     * @param content 所容纳之物
     * @param title   标题
     */
    public static void appendLog(LogTagEnum tag, String title, Object content){

        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(log_path, true));
            String logStr = "["+ DateUtils.getNowTime(1) + "] "+tag.name()+"\n"+title+":"+content;
            bufferedWriter.write(logStr);
            bufferedWriter.newLine();
            bufferedWriter.newLine();
            bufferedWriter.flush();
            bufferedWriter.close();
            System.out.println("A new line has been appended to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred while appending the new line to the file.");
            e.printStackTrace();
        }
    }





}
